/**
 * 
 */
package vwg.na.vwm.rh.motorsdk.modelo.procesos.daoimpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import vwg.na.vwm.rh.motorsdk.modelo.procesos.dao.PadDefinicionBudgetDAO;
import vwg.na.vwm.rh.motorsdk.modelo.procesos.entidadesgrales.PadDefinicionBudget;
import vwg.na.vwm.rh.rhmotor.modelo.dao.GenericDAO;

/**
 * @author Joel Ochoa [joel.ochoa@vw.com.mx]
 *
 */
@Repository
public class PadDefinicionBudgetDAOImpl extends GenericDAO<PadDefinicionBudget> implements
		PadDefinicionBudgetDAO {


	/* (non-Javadoc)
	 * @see vwg.na.vwm.rh.motorsdk.modelo.procesos.dao.PadDefinicionBudgetDAO#getDefinicionBudget(vwg.na.vwm.rh.motorsdk.commons.entidades.UnidadOrganizativa, vwg.na.vwm.rh.motorsdk.commons.entidades.Sociedad, int)
	 * 
	 */
	@Override
	@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
	public PadDefinicionBudget getDefinicionBudget(
			String unidadOrganizativa, String sociedad, int anio) {
		
		StringBuilder query = new StringBuilder("select defBudget");
		query.append(" from PadDefinicionBudget defBudget where ");
		query.append(" defBudget.unidadOrganizativa.unidadOrganizativa = :uo");
		query.append(" and defBudget.sociedad.sociedad = :soc");
		query.append(" and defBudget.anio = :anio");
		
		String[] paramNames = {"uo", "soc", "anio"};
		
		Object[] values = {unidadOrganizativa, sociedad, anio};
		
		@SuppressWarnings("unchecked")
		List<PadDefinicionBudget> defs = getHibernateTemplate().findByNamedParam(query.toString(), paramNames, values);
		
		PadDefinicionBudget definicionBudget;
		
		if(defs == null || defs.size() == 0){
			definicionBudget = null;
		}else{
			definicionBudget = defs.get(0);
		}
		
		return definicionBudget;
	}
	
	@Override
	@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
	public List<PadDefinicionBudget> getDefinicionBudgetHijas(
			String uoCorta, String sociedad, int anio) {
		StringBuilder query = new StringBuilder("select defBudget");
		query.append(" from PadDefinicionBudget defBudget where ");
		query.append(" defBudget.unidadOrganizativa.unidadOrganizativaCorta");
		query.append(" like :uoCorta and defBudget.sociedad.sociedad = :soc");
		query.append(" and defBudget.anio = :anio");
		
		String[] paramNames = {"uoCorta", "soc", "anio"};
		
		Object[] values = {uoCorta + "_", sociedad, anio};
		
		@SuppressWarnings("unchecked")
		List<PadDefinicionBudget> defs = getHibernateTemplate().findByNamedParam(query.toString(), paramNames, values);
		return defs;
	}
}
